#### IAT SCORES FOR AGEISM PROJECT ####


#### 0. DEFINE BASE AND SAVE PATHS ####

# Define where your raw datasets are located
base_path <- "...../Replication_files/v20250801_datasets/Original_data"

script_path <- "..../Replication_files/v20250801_R_Scripts"

# Define where to save the cleaned IAT outputs
save_path <- file.path(script_path, "IAT_results")

# Create save folder if it doesn't exist
if (!dir.exists(save_path)) dir.create(save_path)

#### 1. LOAD LIBRARIES ####
library(iatgen)
library(dplyr)
library(readr)

#### 2. READ DATASETS ####
# Each country's IAT data is read into its own data frame
dat_US <- read.csv(file.path(base_path, "Ageism USA - IAT Round 1_January 11, 2024_14.18.csv"), header = TRUE)

dat_IT <- read.csv(file.path(base_path, "Ageism Italy - IAT Round 1_January 19, 2024_15.19.csv"), header = TRUE)

dat_KOR <- read.csv(file.path(base_path, "Ageism South Korea - IAT Round 1_January 19, 2024_15.21.csv"), header = TRUE)

# Store all datasets in a list so they can be processed in a loop
dats <- list(dat_US, dat_IT, dat_KOR)

# Corresponding country codes for each dataset
country <- c("US", "IT", "KOR")

# Initialize an empty dataframe to store summary statistics for each country
stats_iat <- data.frame()

#### 3. LOOP OVER DATASETS TO PROCESS EACH COUNTRY'S IAT ####

for (i in 1:length(country)) {
  
  # Select the dataset for the current country
  dat <- dats[[i]]
  
  ### STEP 3.1: COLLAPSE RAW IAT RESPONSE BLOCKS INTO FOUR MAIN BLOCKS ###
  # Each call to 'combineIATfourblocks' takes four sub-blocks (e.g., RP4, LP4, RN7, LN7)
  # These represent different pairings of categories used in the IAT structure
  # 'compatible' blocks reflect culturally consistent pairings (e.g., young + good)
  # 'incompatible' blocks reflect less culturally typical pairings (e.g., old + good)
  
  # Collapse four critical blocks for the compatible pairing
  dat$compatible.crit <- combineIATfourblocks(dat$Q4.RP4, dat$Q18.LP4, dat$Q14.RN7, dat$Q28.LN7)
  
  # Collapse four critical blocks for the incompatible pairing
  dat$incompatible.crit <- combineIATfourblocks(dat$Q7.RP7, dat$Q21.LP7, dat$Q11.RN4, dat$Q25.LN4)
  
  # Collapse four practice blocks for the compatible pairing
  dat$compatible.prac <- combineIATfourblocks(dat$Q3.RP3, dat$Q17.LP3, dat$Q13.RN6, dat$Q27.LN6)
  
  # Collapse four practice blocks for the incompatible pairing
  dat$incompatible.prac <- combineIATfourblocks(dat$Q6.RP6, dat$Q20.LP6, dat$Q10.RN3, dat$Q24.LN3)
  
  ### STEP 3.2: CLEAN IAT DATA AND COMPUTE D-SCORES ###
  # 'cleanIAT' applies the Greenwald et al. (2003) scoring algorithm
  # The function returns cleaned data, participant-level D-scores, and quality control stats
  # Setting error.penalty = FALSE assumes forced error correction was enabled (i.e., no need to penalize errors)
  
  clean <- cleanIAT(dat$compatible.prac, dat$compatible.crit,
                    dat$incompatible.prac, dat$incompatible.crit,
                    error.penalty = FALSE)
  
  ### STEP 3.3: COMPUTE SUMMARY STATISTICS FOR QUALITY CHECKS ###
  # Save descriptive statistics for each country's IAT session:
  # - Number of valid participants
  # - Share of timeouts
  # - Number and share of participants removed for excessively fast responses
  # - Internal reliability (split-half method)
  
  d <- data.frame(
    country = country[i],
    n_completed = sum(!clean$skipped),                          # Total number of valid participants
    timeout_drop_rate = clean$timeout.rate,                     # Proportion of responses removed due to timeout
    removed_participants_too_fast_n = clean$fastprt.count,      # Number of participants removed for fast responding
    removed_participants_too_fast_share = clean$fastprt.rate,   # Share of participants removed for that reason
    reliability = IATreliability(clean)$reliability             # Split-half reliability of IAT
  )
  
  # Append the stats for this country to the summary dataframe
  stats_iat <- bind_rows(stats_iat, d)
  
  ### STEP 3.4: EXTRACT INDIVIDUAL D-SCORES AND SAVE TO CSV ###
  # Construct a dataframe with participant-level D-scores
  # Remove rows with missing D-scores (i.e., skipped or excluded participants)
  
  iat <- data.frame(
    ResponseID = dat$ResponseId,
    dscore_iat = clean$D
  ) %>%
    filter(!is.na(dscore_iat))  # Keep only valid participants
  
  # Save the D-score file for this country to the output directory
  write_csv(iat, file.path(save_path, paste0("IAToutput_", country[i], ".csv")))
  
  ### STEP 3.5: PRINT STATUS MESSAGE TO CONSOLE ###
  print(paste0("Done ", country[i]))
}

